﻿$.extend({
    funAction: function (url, paras, callBack) {
        $.post(url, paras, function (data) {
            if (data.Success) {
                if (typeof (callBack) == 'function') {
                    callBack(data.Data);
                }
            }
            else
                alert(data.ErrorMessage);
        });
    },
    funSaveForm: function (url, formId, callBack) {
        if (typeof ($("#" + formId).valid) == "function" && !$("#" + formId).valid()) {
            return;
        }
        var _formId = "#" + formId;
        var mapData = $("#" + formId).serialize();
        $.post(url, mapData, function (d) {
            if (d.Success) {
                if (typeof (callBack) == 'function') {
                    callBack(d.Data);
                }
                else {
                    alert('保存成功');
                }
            }
            else {
                if (typeof (d.ErrorMessage) == 'object') {
                    alert('验证失败')
                }
                else {
                    alert(d.ErrorMessage);
                }
            }
        });
    },
    funDetailSave: function (formId, gridId, editUrl, callBack) {
        //验证表单
        if (typeof ($("#" + formId).valid) == "function" && !$("#" + formId).valid()) {
            return;
        }
        var data = '', notnullColLabels = '';
        var List = new Array();
        var notnullColIndexs = new Array(); //不允许为空的列
        var colModels = $(gridId).getGridParam('colModel');
        $(colModels).each(function (i, c) {
            if (typeof (c.notnull) == 'boolean' && c.notnull) {
                notnullColIndexs.push(c.name);
                notnullColLabels += ',' + (c.label == undefined ? c.name : c.label);
            }
        });
        $(gridId).saveEdittingCell(); //保存正在编辑状态的单元格
        var needDelIdsArray = new Array();
        var ids = $(gridId).getDataIDs();
        for (i = 0; i < ids.length; i++) {
            var flag = false;
            data = $(gridId).getRowData(ids[i]);
            for (var j = 0; j < notnullColIndexs.length; j++) {
                val = eval("data." + notnullColIndexs[j]);
                if (val == null || $.trim(val).length == 0) {
                    if ($.inArray((i + 1), needDelIdsArray) < 0) {
                        needDelIdsArray.push(i + 1);
                    }
                    flag = true;
                }
            }
            if (flag) continue;
            List.push(data);
        }
        if (needDelIdsArray.length > 0) {
            notnullColLabels = notnullColLabels.substring(1);
            var _alertMsg = "警告：明细行 " + notnullColLabels + " 不允许为空，";
            _alertMsg += "行 " + needDelIdsArray.toString() + " 不符合指定的要求。";
            _alertMsg += "选择“确定” 系统会自动删除不符合要求的行并执行保存，选择“取消”取消本次保存操作。";
            if (!confirm(_alertMsg)) return;
            for (var i = needDelIdsArray.length - 1; i >= 0; i--) {//删除不合法的行
                $(gridId).delRowData(needDelIdsArray[i]);
            }
        }
        var postData = {
            detail: $("#" + formId).serialize(),
            dataList: JSON.stringify(List)
        };
        $.post(editUrl, postData, function (d) {
            if (d.Success == undefined) {
                if (d[0] == null || d[0] == "") {
                    alert("操作成功！");
                }
                else {
                    alert(d[0]);
                }
                return;
            }
            if (d.Success) {
                alert("操作成功！");
                //回调
                if (typeof (callBack) == "function") {
                    callBack(d.Data);
                }
            }
            else {
                if (typeof (d.ErrorMessage) == 'object') {
                    var html = '';
                    $(d.ErrorMessage).each(function (key, val) {
                        html += "-" + val + "<br />";
                    });
                    $.Zebra_Dialog(html, {
                        'type': 'error',
                        'title': '验证失败',
                        'modal': false
                    });
                }
                else {
                    alert(d.ErrorMessage);
                    if (typeof (callBack) == "function") {
                        callBack(d);
                    }
                }
            }
        });
    },
    funListDelete: function (gridId, pKey, deleteUrl, callBack) {
        var selIds = jqGridHelper.getSelRowsData(gridId, pKey);
        if (selIds == null || selIds.length == 0) {
            selIds = jqGridHelper.getSelRowData(gridId, pKey);
            if (selIds == null || selIds.length == 0) {
                alert('没有选中的数据行');
                return;
            }
        }
        var selLength = selIds.split(',').length; //总行数
        if (confirm("确认要删除选中的 " + selLength + " 行数据吗？")) {
            $.post(deleteUrl, { ids: selIds }, function (d) {
                if (d.Success == undefined) {
                    if (d >= 1 || d == "") {
                        alert("操作成功！");
                        $(gridId).trigger("reloadGrid");
                    }
                    else
                        alert(d);
                    return;
                }
                if (d.Success) {
                    alert("操作成功！");
                    //回调
                    if (typeof (callBack) == "function") {
                        callBack(d.Data);
                    }
                    $(gridId).trigger("reloadGrid");
                }
                else {
                    alert(d.ErrorMessage);
                }
            });
        }
    }
});

//格式化金额
function formatFloat(src, pos) {
    return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}

function formatNumber(number, decimalPlaces) {
    number = number.toString().replace(/\,/g, "");
    if (isNaN(number) || number == "") return "";
    if (decimalPlaces == 0) {
        return parseInt(number);
    }
    var _decimalPlaces = 2;
    if (typeof (decimalPlaces) == 'number') {
        _decimalPlaces = decimalPlaces > 12 ? 12 : decimalPlaces; //最大12位
    }
    number = Math.round(number * Math.pow(10, _decimalPlaces)) / Math.pow(10, _decimalPlaces);
    if (number < 0)
        return '-' + formatDollars(Math.floor(Math.abs(number) - 0) + '') + formatCents(Math.abs(number) - 0, _decimalPlaces);
    else
        return formatDollars(Math.floor(number - 0) + '') + formatCents(number - 0, _decimalPlaces);
}
function formatDollars(number) {
    if (number.length <= 3)
        return (number == '' ? '0' : number);
    else {
        var mod = number.length % 3;
        var output = (mod == 0 ? '' : (number.substring(0, mod)));
        for (i = 0; i < Math.floor(number.length / 3); i++) {
            if ((mod == 0) && (i == 0))
                output += number.substring(mod + 3 * i, mod + 3 * i + 3);
            else
                output += number.substring(mod + 3 * i, mod + 3 * i + 3);
        }
        return (output);
    }
}
function formatCents(amount, decimalPlaces) {
    var _decimalPlaces = 0;
    if (typeof (decimalPlaces) == 'number') {
        _decimalPlaces = decimalPlaces;
    }
    if (decimalPlaces == 0) return "";
    amount = Math.round((amount - Math.floor(amount)) * Math.pow(10, _decimalPlaces)) / Math.pow(10, _decimalPlaces);
    if (amount == 0) {
        return ".".PadRight(_decimalPlaces + 1, '0');
    }
    if (amount.toString().substring(2).length < _decimalPlaces) {
        if (amount.toString().substring(2).length < 2)
            return amount.toString().substring(1).PadRight(3, '0');
        return amount.toString().substring(1);
    }
    return amount.toString().substring(1).PadRight(_decimalPlaces + 1, '0');
}

String.prototype.PadLeft = function (totalWidth, paddingChar) {
    if (paddingChar != null) {
        return this.PadHelper(totalWidth, paddingChar, false);
    } else {
        return this.PadHelper(totalWidth, ' ', false);
    }
}
String.prototype.PadRight = function (totalWidth, paddingChar) {
    if (paddingChar != null) {
        return this.PadHelper(totalWidth, paddingChar, true);
    } else {
        return this.PadHelper(totalWidth, ' ', true);
    }
}
String.prototype.PadHelper = function (totalWidth, paddingChar, isRightPadded) {
    if (this.length < totalWidth) {
        var paddingString = new String();
        for (i = 1; i <= (totalWidth - this.length); i++) {
            paddingString += paddingChar;
        }

        if (isRightPadded) {
            return (this + paddingString);
        } else {
            return (paddingString + this);
        }
    } else {
        return this;
    }
}




$.fn.extend({
    serializeJson: function () {
        var serializeObj = {};
        $(this.serializeArray()).each(function () {
            serializeObj[this.name] = this.value;
        });
        return serializeObj;
    }
});
